/**
 * MoteApp.java
 * 
 * @author Andrew Moss 
 * 
 * Created on 1 Oct 2010
 *
 * This file contains the MoteApp class
 */

package com.mossdev.android.moteapp;

import com.mossdev.android.moteapp.comms.ServerConnection;
import com.mossdev.android.moteapp.comms.ServerConnectionException;
import com.mossdev.android.moteapp.comms.ServerConnectionState;

import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;

/**
 * The MoteApp class is the root activity for the Mote Android Application.
 * It extends TabActivity as the interface makes use of the tab widgets.
 * 
 * @author Andrew Moss
 *
 */
public class MoteApp extends TabActivity {
	
	protected static ServerConnection servercomms;
	protected static Config config;
	
	/**
	 *  Called when the MoteApp activity is first created.
	 *  Creates the tab host and adds the appropriate activity to each.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    //Content view is specified by main.xml
	    setContentView(R.layout.main);

	    //Get user's configuration
	    SharedPreferences prefs = getSharedPreferences("MoteAppConfig", MODE_PRIVATE);
        config = new Config(prefs);
        
        //Try and connect to the server
        servercomms = new ServerConnection(config);
	    try {
        	servercomms.connect();
        } catch(ServerConnectionException e) {
        	//error connecting to the server
        	Log.e(Config.LogTag, e.err_msg);
        	Log.e(Config.LogTag, e.cause.getLocalizedMessage());
        }

	    //The following code is straight out of the android tab example
	    //It creates the tab interface.
	    //-----------------------------
	    Resources res = getResources();
	    TabHost tabHost = getTabHost(); 
	    TabHost.TabSpec spec;
	    Intent intent; 

	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, MoteAppNavActivity.class);

	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec("nav").setIndicator("Nav",
	                      res.getDrawable(R.drawable.tab_nav))
	                  .setContent(intent);
	    
	    tabHost.addTab(spec);

	    // Do the same for the other tabs
	    intent = new Intent().setClass(this, MoteAppTouchpadActivity.class);
	    spec = tabHost.newTabSpec("touchpad").setIndicator("Touchpad",
	                      res.getDrawable(R.drawable.tab_touchpad))
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    
	    intent = new Intent().setClass(this, MoteAppCustomActivity.class);
	    spec = tabHost.newTabSpec("custom").setIndicator("Custom",
	                      res.getDrawable(R.drawable.cust_nav))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    intent = new Intent().setClass(this, MoteAppSettingsActivity.class);
	    spec = tabHost.newTabSpec("settings").setIndicator("Settings",
	                      res.getDrawable(R.drawable.tab_settings))
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    
	    TabWidget tw = getTabWidget();
	    for (int i = 0; i < tw.getChildCount(); i++) {
			View v = tw.getChildAt(i);
			v.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.moteapp_tab_profile));
		} 

	    tabHost.setCurrentTab(3); //Settings tab is the starting tab
	    //-----------------------------
	    
	}

	/**
	 * Generates the options menu when first requested by the user.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.optionmenu, menu);
	    return true;
	}
	
	/**
	 * Handle button presses in the options menu
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.quit:
	    	finish();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
    protected void onDestroy()
    {
    	if (servercomms.getConnectionState() != ServerConnectionState.DISCONNECTED ) servercomms.disconnect();
    	super.onDestroy();
    }
	
	@Override
	protected void onStop() {
		if (servercomms.getConnectionState() != ServerConnectionState.DISCONNECTED ) servercomms.disconnect();
		super.onStop();
	}


}